package com.timoon.component.panel.tree.treetable;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation;
import org.apache.wicket.extensions.markup.html.tree.table.IColumn;
import org.apache.wicket.extensions.markup.html.tree.table.PropertyTreeColumn;

/**
 * 将ThreeColumnTabelPanel第二列改成tree
 * @author Administrator
 *
 */
public abstract class ThreeColumnTreeTablePanel extends ThreeColumnTabelPanel {
	private static final long serialVersionUID = 1447403614192501720L;
	
	public ThreeColumnTreeTablePanel(String id, List<Object> data,
			List<String> checkedIds, Map<String, String> configMap) {
		super(id, data, checkedIds, configMap);
		// TODO Auto-generated constructor stub
		
	}
	@Override
	protected IColumn getColumn1(ColumnLocation location,
			String title, String expression) {
		// TODO Auto-generated method stub
		PropertyTreeColumn column=new PropertyTreeColumn<>(location, title, expression);
		return column;
	}
	@Override
	protected TreeModel buildTreeModel(List<Object> data) {
		// TODO Auto-generated method stub
		DefaultMutableTreeNode root=new DefaultMutableTreeNode(this.getRootObject());
		
		String parentKey=this.getParentKey();
		
		/**父id，子对象集合*/
		Map<String,List<Object>> objMap=new HashMap<String,List<Object>>();
		if(data!=null){
			for(Object obj:data){
				try {
					//构建objMap
					String parentValue=BeanUtils.getProperty(obj, parentKey);
					
					List<Object> objs=objMap.get(parentValue);
					if(objs==null){
						objs=new ArrayList<Object>();
					}
					objs.add(obj);
					objMap.put(parentValue, objs);
					
				} catch (IllegalAccessException | InvocationTargetException
						| NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
		root=this.buildNode(objMap, root, parentKey);
		return new DefaultTreeModel(root);
	}
	
	private DefaultMutableTreeNode buildNode(Map<String,List<Object>> objMap,DefaultMutableTreeNode parentNode,String parentKey){
		String parentValue=null;
		try {
			parentValue = BeanUtils.getProperty(parentNode.getUserObject(), "id");
		} catch (IllegalAccessException | InvocationTargetException
				| NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		List<Object> childObjs=objMap.get(parentValue);
		if(childObjs!=null){
			for(Object obj:childObjs){
				DefaultMutableTreeNode childNode=new DefaultMutableTreeNode(obj);
				parentNode.add(childNode);
				this.buildNode(objMap, childNode, parentKey);
			}
		}
		
		return parentNode;
	}
	/**
	 * 获取条目里表征parent_id的字段
	 */
	protected abstract String getParentKey();
	
	/**
	 * 获取一个root节点对象<br>
	 * 注意：id属性=0
	 * @return
	 */
	protected abstract Object getRootObject();

}
